In [4]:
import numpy as np

In [5]:
x = [1,2,3]
y = [4,5,6]
x + y


Out[5]:
[1, 2, 3, 4, 5, 6]

In [6]:
B = np.array([[1,2,3], [4,5,6]]) # habiendo corrido import numpy as np

In [7]:
B + 2*B # Python sabe sumar y multiplicar arrays como algebra lineal


Out[7]:
array([[ 3,  6,  9],
       [12, 15, 18]])

In [8]:
np.matmul(B.transpose(), B) # B^t*B


Out[8]:
array([[17, 22, 27],
       [22, 29, 36],
       [27, 36, 45]])

In [9]:
B[1,1]


Out[9]:
5

In [10]:
B[1,:]


Out[10]:
array([4, 5, 6])

In [11]:
B[:,2]


Out[11]:
array([3, 6])

In [12]:
B[0:2,0:2]


Out[12]:
array([[1, 2],
       [4, 5]])

In [13]:
B.shape


Out[13]:
(2, 3)

In [14]:
vec = np.array([1,2,3])
print(vec)


[1 2 3]

TAREA


In [62]:
class Array:
    "Una clase minima para algebra lineal"
    def __init__(self, list_of_rows): 
        "Constructor"
        self.data = list_of_rows
        self.shape = (len(list_of_rows), len(list_of_rows[0]))

In [63]:
A = Array([[1,2,3], [4,5,6]])
A.__dict__ # el campo escondido __dict__ permite acceder a las propiedades de clase de un objeto


Out[63]:
{'data': [[1, 2, 3], [4, 5, 6]], 'shape': (2, 3)}

In [64]:
A.data


Out[64]:
[[1, 2, 3], [4, 5, 6]]

In [65]:
A.shape


Out[65]:
(2, 3)

1.Un metodo para imprimir mejor


In [1]:
class Array:
    "Una clase minima para algebra lineal"    
    def __init__(self, list_of_rows): 
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")
            
    def __repr__(self):
        return "\n".join(str(x) for x in self.data)
    
    def __str__(self):
        return "\n".join(repr(x) for x in self.data)

In [2]:
A = (Array([[1,2,3], [4,5,6]]))

In [5]:
print(A)


[1, 2, 3]
[4, 5, 6]

In [6]:
A


Out[6]:
[1, 2, 3]
[4, 5, 6]

2. Un validador


In [7]:
class Array:
    "Una clase minima para algebra lineal"    
    def __init__(self, list_of_rows): 
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")
    
    def __getitem__(self, idx):
        return self.data[idx[0]][idx[1]]

In [8]:
A=Array([[2,6,12],[5,10,15]])
A[0,2]


Out[8]:
12

In [9]:
class Array:
    "Una clase minima para algebra lineal"    
    def __init__(self, list_of_rows): 
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")
    
    def __setitem__(self, idx, value):
        self.data[idx[0]][idx[1]] = value
        
    def __repr__(self):
        return "\n".join(str(x) for x in self.data)
    
    def __str__(self):
        return "\n".join(repr(x) for x in self.data)

In [10]:
A=Array([[2,6,12],[5,10,15]])
A[0,2]=8

In [12]:
A.data


Out[12]:
[[2, 6, 8], [5, 10, 15]]

In [14]:
print(A)


[2, 6, 8]
[5, 10, 15]

3. Indexing and Item assignment


In [15]:
A = Array([[1,2], [3,4]])
A[0,0]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-59057898ec49> in <module>()
      1 A = Array([[1,2], [3,4]])
----> 2 A[0,0]

TypeError: 'Array' object is not subscriptable

In [16]:
##ejercicio 3.1
def zeros (x, y): 
    zeros = [[0 for x in range(x)] for y in range(y)]
    return zeros

In [18]:
M=zeros(5,5)

In [19]:
print(M)


[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

In [20]:
zeros(5,5)


Out[20]:
[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]
Matriz Identidad

In [21]:
#Ejercicio 3.2
def eye(n):
    matrix = [[0]*n for i in range(n)]
    for i in range(n):
        matrix[i][i] = 1
    return matrix

In [22]:
eye(5)


Out[22]:
[[1, 0, 0, 0, 0],
 [0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 0, 1, 0],
 [0, 0, 0, 0, 1]]

5. Transposicion


In [115]:
np.array([[1,2], [3,4]]).transpose()


Out[115]:
array([[1, 3],
       [2, 4]])

EJERCICIO 4


In [121]:
matrix=[[1,2],[3,4]]

In [123]:
[[row[i] for row in matrix] for i in range(2)]


Out[123]:
[[1, 3], [2, 4]]

6. Suma


In [124]:
"hola " + "tu"


Out[124]:
'hola tu'

In [125]:
[1,2,3] + [2,3,4]


Out[125]:
[1, 2, 3, 2, 3, 4]

In [157]:
np.array([1,2,3]) + np.array([2,3,4])


Out[157]:
array([3, 5, 7])

In [127]:
np.array([1,2,3]) + 10 # Broadcasted sum, es muy util


Out[127]:
array([11, 12, 13])

In [128]:
class Array:
    "Una clase minima para algebra lineal"    
    def __init__(self, list_of_rows): 
        "Constructor y validador"
        # obtener dimensiones
        self.data = list_of_rows
        nrow = len(list_of_rows)
        #  ___caso vector: redimensionar correctamente
        if not isinstance(list_of_rows[0], list):
            nrow = 1
            self.data = [[x] for x in list_of_rows]
        # ahora las columnas deben estar bien aunque sea un vector
        ncol = len(self.data[0])
        self.shape = (nrow, ncol)
        # validar tamano correcto de filas
        if any([len(r) != ncol for r in self.data]):
            raise Exception("Las filas deben ser del mismo tamano")
        
    def __add__(self, other):
        "Hora de sumar"
        if isinstance(other, Array):
            if self.shape != other.shape:
                raise Exception("Las dimensiones son distintas!")
            rows, cols = self.shape
            newArray = Array([[0. for c in range(cols)] for r in range(rows)])
            for r in range(rows):
                for c in range(cols):
                    newArray.data[r][c] = self.data[r][c] + other.data[r][c]
            return newArray
        elif isinstance(2, (int, float, complex)): # en caso de que el lado derecho sea solo un numero
            rows, cols = self.shape
            newArray = Array([[0. for c in range(cols)] for r in range(rows)])
            for r in range(rows):
                for c in range(cols):
                    newArray.data[r][c] = self.data[r][c] + other
            return newArray
        else:
            return NotImplemented # es un tipo de error particular usado en estos metodos

In [155]:
A = Array([[1,2], [3,4]])
B = Array([[5,6], [7,8]])
C = A + B
C.data


Out[155]:
[[6, 8], [10, 12]]

In [153]:
D = A + 10

In [131]:
D.data


Out[131]:
[[11, 12], [13, 14]]